import com.google.appengine.api.datastore.Entity
import com.google.appengine.api.datastore.Text
import com.google.appengine.api.datastore.Category
import com.ciandt.snippetr.Snippet

def snippet = new Snippet();
snippet.name = params.name;
snippet.language = params.language;
snippet.text = params.text as Text;

if (user) {
	snippet.author = user 
}

if (params.tags) {
	def tags = []
	params.tags.split().each { tag ->
		tags << (tag as Category)
	}
	snippet.tags = tags
}

// coerce a POJO into an entity
def snippetEntity = snippet as Entity
snippetEntity.save()

if ('latestSnippets' in memcache) {
	def latestSnippets = memcache['latestSnippets']
	latestSnippets[0..<0] = [snippetEntity]
	memcache['latestSnippets'] = latestSnippets 
}

request['snippet'] = snippetEntity
redirect "/snippet/${snippetEntity.key.id}"